package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Override
    public void add(SetmealDTO dto) {
        // 添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.add(setmeal);

        // 添加菜品
        List<SetmealDish> setmealList = dto.getSetmealDishes();
        if (setmealList.size() > 0) {
            setmealList.forEach(list -> {
                list.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.add(setmealList);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        // 分页
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page p = setmealMapper.page(dto);
        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 套餐停售启售
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        // 创建套餐对象
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDTO findById(Long id) {
        // 创建回显对象
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmealMapper.findById(id), setmealDTO);
        setmealDTO.setSetmealDishes(setmealDishMapper.findById(id));
        return setmealDTO;
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Override
    public void upadte(SetmealDTO dto) {
        // 创建套餐对象
        Setmeal setmeal = new Setmeal();
        // 搬运数据
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);
        // 先把套餐里原来的菜品全删除
        setmealDishMapper.delete(Arrays.asList(dto.getId()));
        // 在把修改后的菜品添加进去
        List<SetmealDish> setmealDishList = dto.getSetmealDishes();

        if (setmealDishList.size() > 0) {
            setmealDishList.forEach(id -> {
                id.setSetmealId(dto.getId());
            });
            setmealDishMapper.add(setmealDishList);
        }
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public void delete(List<Long> ids) {
        // 遍历循环id
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.findById(id);
            // 判断是否有在售的套餐
            if (setmeal.getStatus() == 1) {
                // 有就抛出异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
